import { rgbToHex } from "/handlers/rgbToHex.js";
import { saveNote } from "/handlers/saveNote.js";
const contentHandler = {
    whenBlur: () => {
        let issaved = document.getElementsByClassName('issaved')[0];
        let istyping = document.getElementsByClassName('istyping')[0];
        let elNid = document.getElementById('nid-box');
        let elTitle = document.getElementById('title');
        let elContent = document.getElementById('content');
        elTitle.addEventListener("blur", function () {
            let nid = elNid.innerHTML;
            let liid = 'li' + nid;
            let content = elContent.value;
            let title = elTitle.value;
            let elList = document.getElementById(liid);
            let right_container = document.getElementsByClassName('right-container')[0];
            let rgbColor = window.getComputedStyle(right_container).backgroundColor;
            let currentBgColor = rgbToHex(rgbColor);
            if (saveNote(nid, content, title, currentBgColor)) {
                elList.firstElementChild.innerHTML = title;
                istyping.setAttribute("id", "istyping");
                issaved.setAttribute("id", "saved");
            } else {
                Qmsg.error('保存失败！');
            }
        });
        elContent.addEventListener("blur", function () {
            let nid = elNid.innerHTML;
            let content = elContent.value;
            let title = elTitle.value;
            let right_container = document.getElementsByClassName('right-container')[0];
            let rgbColor = window.getComputedStyle(right_container).backgroundColor;
            let currentBgColor = rgbToHex(rgbColor);
            if (saveNote(nid, content, title, currentBgColor)) {
                istyping.setAttribute("id", "istyping");
                issaved.setAttribute("id", "saved");
            } else {
                Qmsg.error('保存失败！');
            }
        });
    },
    whenFocus: () => {
        let issaved = document.getElementsByClassName('issaved')[0];
        let istyping = document.getElementsByClassName('istyping')[0];
        content.addEventListener("focus", function () {
            istyping.setAttribute("id", "istyping");
            istyping.setAttribute("id", "typing");
            issaved.removeAttribute("id");
            issaved.setAttribute("id", "issaved");
        });
        title.addEventListener("focus", function () {
            istyping.setAttribute("id", "istyping");
            istyping.setAttribute("id", "typing");
            issaved.removeAttribute("id");
            issaved.setAttribute("id", "issaved");
        });
    }
}
export default contentHandler